/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClientFrame.java
 *
 * Created on 25-Mar-2011, 20:01:06
 */
package view;

import client.Client;
import com.sun.media.sound.JavaSoundAudioClip;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JList;
import model.Message;

/**
 *
 * @author CUNEYT
 */
public class ClientFrame extends javax.swing.JFrame implements Observer {

    private Client client;
    private JavaSoundAudioClip sound;
    private int selected = Message.SIGNAL;
    private String username;
    private String password;

    /** Creates new form ClientFrame */
    public ClientFrame(String username, String password) throws NullPointerException {
        initComponents();
        this.username = username;
        this.password = password;
        init();
    }

    private void init() throws NullPointerException {
        try {
            client = new Client(username, password, this);
            client.addObserver(this);
            client.start();
            sound = new JavaSoundAudioClip(this.getClass().getResourceAsStream("newMessage.wav"));
            messageList.addMouseListener(new ActionJList(messageList));
            updateMessageList();
            setLocationRelativeTo(null);
        } catch (IOException ex) {
            Logger.getLogger(ClientFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        playSoundCheckBox = new javax.swing.JCheckBox();
        messagesScrollPane = new javax.swing.JScrollPane();
        messageList = new javax.swing.JList();
        viewSignalsButton = new javax.swing.JButton();
        commentsButton = new javax.swing.JButton();
        reconnectButton = new javax.swing.JButton();
        clock1 = new view.Clock();
        logo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sinyaller ve Yorumlar");
        setBackground(new java.awt.Color(29, 29, 29));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        mainPanel.setBackground(new java.awt.Color(29, 29, 29));

        playSoundCheckBox.setBackground(new java.awt.Color(29, 29, 29));
        playSoundCheckBox.setForeground(new java.awt.Color(255, 255, 255));
        playSoundCheckBox.setText("Sesli Uyarı");

        messageList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        messageList.setCellRenderer(new MessageListCellRenderer());
        messagesScrollPane.setViewportView(messageList);

        viewSignalsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/signal.png"))); // NOI18N
        viewSignalsButton.setText("Sinyaller");
        viewSignalsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewSignalsButtonActionPerformed(evt);
            }
        });

        commentsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/comment.png"))); // NOI18N
        commentsButton.setText("Anlık Yorumlar");
        commentsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                commentsButtonActionPerformed(evt);
            }
        });

        reconnectButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/socket.png"))); // NOI18N
        reconnectButton.setText("Bağlantı Yenile");
        reconnectButton.setEnabled(false);
        reconnectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reconnectButtonActionPerformed(evt);
            }
        });

        clock1.setForeground(new java.awt.Color(255, 255, 255));

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/logo2.jpg"))); // NOI18N
        logo.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        logo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                logoMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(messagesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 605, Short.MAX_VALUE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(viewSignalsButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(commentsButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 170, Short.MAX_VALUE)
                        .addComponent(reconnectButton))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(playSoundCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                        .addComponent(logo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(clock1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(viewSignalsButton)
                    .addComponent(commentsButton)
                    .addComponent(reconnectButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(messagesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                .addGap(10, 10, 10)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(playSoundCheckBox)
                        .addComponent(logo))
                    .addComponent(clock1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(36, 36, 36))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void viewSignalsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewSignalsButtonActionPerformed
        selected = Message.SIGNAL;
        viewSignalsButton.setBackground(Color.white);
        updateMessageList();
    }//GEN-LAST:event_viewSignalsButtonActionPerformed

    private void commentsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_commentsButtonActionPerformed
        selected = Message.COMMENT;
        commentsButton.setBackground(Color.white);
        updateMessageList();
    }//GEN-LAST:event_commentsButtonActionPerformed

    private void reconnectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reconnectButtonActionPerformed
        client.connect();
    }//GEN-LAST:event_reconnectButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        client.finish();
    }//GEN-LAST:event_formWindowClosing

    private void logoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_logoMouseClicked

        try {
            URI url = new URI("http://www.orenux.com/forum");
            java.awt.Desktop.getDesktop().browse(url);
            /*JEditorPane output = new JEditorPane();
            output.setContentType("text/html");
            output.setEditable(false);
            output.setPage( "http://mindprod.com" );*/
        } catch (IOException ex) {
            Logger.getLogger(ClientFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (URISyntaxException ex) {
            Logger.getLogger(ClientFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_logoMouseClicked

    private void updateMessageList() {
        if (selected == Message.SIGNAL) {
            messageList.setModel(new MessageModel(client.getSignals()));
        } else {
            messageList.setModel(new MessageModel(client.getComments()));
        }
        //messageList.ensureIndexIsVisible(messageList.getModel().getSize() - 1);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private view.Clock clock1;
    private javax.swing.JButton commentsButton;
    private javax.swing.JLabel logo;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JList messageList;
    private javax.swing.JScrollPane messagesScrollPane;
    private javax.swing.JCheckBox playSoundCheckBox;
    private javax.swing.JButton reconnectButton;
    private javax.swing.JButton viewSignalsButton;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object arg) {
        if (playSoundCheckBox.isSelected()) {
            sound.play();
        }
        if ((Integer) arg != selected) {
            if (selected == Message.COMMENT) {
                viewSignalsButton.setBackground(Color.red);
            } else {
                commentsButton.setBackground(Color.red);
            }
        }
        updateMessageList();
    }

    class ActionJList extends MouseAdapter {

        protected JList list;

        public ActionJList(JList l) {
            list = l;
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                Message msg = (Message) list.getSelectedValue();
                new MessageDialog(null, true, msg).setVisible(true);
//                JTextArea ta = new JTextArea(msg.getContent());
//                ta.setLineWrap(true);
//                JScrollPane sp = new JScrollPane(ta);
//                sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//                sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//                JOptionPane.showMessageDialog(null, sp, msg.getDate() + " " + msg.getTime(), JOptionPane.PLAIN_MESSAGE);
            }
        }
    }

    public void setConnectionButtonVisible(boolean aFlag) {
        reconnectButton.setEnabled(aFlag);
    }
}
